// var num_16 = 'CB0042AA';
// var num_16 = 'BF866666';
// var num_16 = 'BFCA3D70';
var num_16 = '42aac800';
var float_number_10 = exchange16to10(num_16);
console.log(float_number_10)


/**
 * 16进制转化成10进制
 * @param num_16
 * @returns {*}
 */
function exchange16to10(num_16) {
    var num_2 = exchange16to2(num_16)
    console.log(num_2);
    var float_num_2  = exchange2toFloat(num_2);
    console.log(float_num_2);

    var flag  = flag_1(num_2);
    if(float_num_2 ==0){
        return 0;
    }
    if(flag == 1){
        return -1*exchange2to10(float_num_2);
    }else{
        return exchange2to10(float_num_2)
    }
}


/**
 * 第一步：将16进制转化成2进制
 * @param num_16
 * @returns {string}
 */
function exchange16to2(num_16){
    if(num_16.length < 8){
        num_16 =  (num_16 + Array(8).join(0)).slice(0, 8);
    }
    var hex_array = [{key:0,val:"0000"},{key:1,val:"0001"},{key:2,val:"0010"},{key:3,val:"0011"},{key:4,val:"0100"},{key:5,val:"0101"},{key:6,val:"0110"},{key:7,val:"0111"},
        {key:8,val:"1000"},{key:9,val:"1001"},{key:'a',val:"1010"},{key:'b',val:"1011"},{key:'c',val:"1100"},{key:'d',val:"1101"},{key:'e',val:"1110"},{key:'f',val:"1111"}]

    var value=""
    for(let i=0;i<num_16.length;i++){
        for(let j=0;j<hex_array.length;j++){
            if(num_16.charAt(i).toLowerCase()== hex_array[j].key){
                value = value.concat(hex_array[j].val)
                break
            }
        }
    }
    return value;
}

/**
 * 第二步：将2进制转化成浮点数形式
 * @param num_2
 * @returns {number}
 */
function exchange2toFloat(num_2){
    var copy = num_2,flag = num_2.substring(0,1),jie = num_2.substring(1,9),rest = num_2.substring(9),val='';

    if(rest.indexOf("1") != -1 ){
        var jingdu = '1.'+num_2.substring(9);
    }
    var jie_val = 0;

    for(let i=0;i<jie.length;i++){
        jie_val +=jie.charAt(i)*Math.pow(2, 7-i)
    }
    jie_val = jie_val-127;
    if(jie_val >0){
        if(jie_val < 23){
            val = '1'+rest.slice(0,jie_val)+'.'+rest.slice(jie_val) ;
        }else{
            val = '1'+(rest + Array(jie_val-23).join(0)).slice(0, jie_val);
        }
    }else if(jie_val < 0){
        if(jie_val == -126){
            console.log(-jie_val+23)
            console.log((new Array(-jie_val+1).join('0') + rest).slice(0, -jie_val+23).length)
            console.log(-(-jie_val+23)+1)
            // val = '0.'+(new Array(-jie_val+1).join('0') + rest).slice(-(-jie_val+23));
            val = '0.'+(new Array(-jie_val).join('0') + rest).slice(0);
        }else{
            val = '1.'+(new Array(-jie_val).join('0') + rest).slice(0);
        }

    }else{
        val = jingdu;
    }

    // val =   jingdu * Math.pow(10, jie_val);
    console.log(rest);
    console.log(val);
    console.log(jie_val);
    // var num = val.toLocaleString().replace(/,/g,'');
    // return num;
    return  val;
}

function flag_1(num_2){
    return  num_2.substring(0,1);
}
/**
 * 第三步：将2进制浮点数形式转化成10进制数
 * @param binaryNum
 * @returns {number|*}
 */
function exchange2to10(binaryNum){
    const binaryFloatNumArr = binaryNum.toString().split(".")
    const binaryIntPartArr = binaryFloatNumArr[0].split("")
    var eachDecimalIntPartNum = eachBinaryIntPartToDecimal(binaryIntPartArr)
    var decimalIntPartNum = eachDecimalIntPartNum.reduce((accumulator, currentValue) => { return accumulator + currentValue })
    console.log(decimalIntPartNum)
    console.log(binaryFloatNumArr[1])
    var deciamlFloatPartNum = 0
    // const decimalIntPartNum = parseInt(binaryIntParStr, 2)
    // 将二进制小数部分转换为十进制数
    if(binaryFloatNumArr[1]){
        const binaryFloatPartArr = binaryFloatNumArr[1].split("")
        const eachDecimalFloatPartNum = eachBinaryFloatPartToDecimal(binaryFloatPartArr)
        // console.log(eachDecimalFloatPartNum)
        deciamlFloatPartNum = eachDecimalFloatPartNum.reduce((accumulator, currentValue) => { return accumulator + currentValue })
    }
    return decimalIntPartNum + deciamlFloatPartNum
}

function eachBinaryIntPartToDecimal(binaryFloatPartArr) {
    return binaryFloatPartArr.map((currentValue, index) => {
        return Number(currentValue) * Math.pow(2, (binaryFloatPartArr.length-index-1))
    })
}

function eachBinaryFloatPartToDecimal(binaryFloatPartArr) {
    return binaryFloatPartArr.map((currentValue, index) => {
        return Number(currentValue) * Math.pow(2, (-(index + 1)))
    })
}
